package com.yycx.module.bus.provider.handler.huikuan;

import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.constants.CommonConstants;
import com.yycx.common.mybatis.interceptor.SaveInterceptor;
import com.yycx.common.mybatis.query.CriteriaSave;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.utils.ApiAssert;
import com.yycx.common.utils.DateUtils;
import com.yycx.module.bus.client.entity.BusOrder;
import com.yycx.module.bus.client.entity.BusOrderDetails;
import com.yycx.module.bus.client.entity.BusPayment;
import com.yycx.module.bus.client.enums.AmountTypeEnum;
import com.yycx.module.bus.provider.service.BusOrderDetailsService;
import com.yycx.module.bus.provider.service.BusOrderService;
import com.yycx.module.bus.provider.service.BusPaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

/**
 * 回款保存扩展器
 *
 * @author zyf
 */
@Component("huikuanPaymentSaveHandler")
public class HuiKuanPaymentSaveHandler implements SaveInterceptor<BusPayment> {

    @Autowired
    private BusPaymentService paymentService;

    @Autowired
    private BusOrderService orderService;

    @Autowired
    private BusOrderDetailsService orderDetailsService;


    @Override
    public void prepare(CriteriaSave cs, EntityMap entityMap, BusPayment payment) {
        String orderDetailsId=payment.getOrderDetailsId();
        BusPayment check = paymentService.getByOrderDetailsIdAndAmountType(orderDetailsId, AmountTypeEnum.CustomRefund);
        if (FlymeUtils.isNotEmpty(check)) {
            ApiAssert.failure("请不要重复回款");
        }
        String returnAmount=cs.getParams("returnAmount");
        BusOrderDetails orderDetails=orderDetailsService.getById(orderDetailsId);
        BusOrder order=orderService.getById(orderDetails.getOrderId());
        payment.setOrganizationId(orderDetails.getOrganizationId());
        payment.setCompanyId(orderDetails.getCompanyId());
        payment.setDeptId(orderDetails.getDeptId());
        payment.setTitle("客户回款");
        payment.setAmount(new BigDecimal(returnAmount));
        payment.setOrderId(orderDetails.getOrderId().toString());
        payment.setPayDate(payment.getAmountDate());
        payment.setOrderDetailsId(orderDetails.getOrderDetailsId().toString());
        payment.setSupplierId(orderDetails.getSupplierId());
        payment.setCustomId(orderDetails.getCustomId().toString());
        payment.setAmountType(AmountTypeEnum.CustomRefund.getCode());
        String orderNo = order.getOrderNo();
        payment.setPaymentNo("HK" + orderNo);
        payment.setPrintTag(CommonConstants.DISABLED);
        payment.setUserId(OpenHelper.getUserId());
        payment.setNickName(OpenHelper.getNickName());
        payment.setPaymentState(CommonConstants.INT_0);
    }

    @Override
    public void complete(CriteriaSave cs, EntityMap params, BusPayment entity) {
        String orderDetailsId = entity.getOrderDetailsId();
        //管理回款ID
        orderDetailsService.updateHuiKuan(entity,Long.parseLong(orderDetailsId));
    }

}
